<?php
/**
 * PlayerAlias Data Accessor for CTRT_RaidTracker
 *
 * @category Plugins
 * @package CT_RaidTrackerImport
 * @copyright (c) 2006, EQdkp <http://www.eqdkp.com>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 *
 * @author Garrett Hunter <loganfive@blacktower.com>
 * $Rev: 84 $ $Date: 2007-04-22 09:42:16 +0000 (Sun, 22 Apr 2007) $
 */

if ( !defined('EQDKP_INC') )
{
    die('Haxor!!!');
}

include_once('dataaccessor.php');

/**
 * CRUD Class PlayerAlias table
 * @subpackage CTRTDataAccessors
 */
class CTRT_PlayerAlias extends CTRT_DataAccessor {

	var $_alias_name;
	
	function CTRT_PlayerAlias () { 

		$this->setMyTable(CTRT_ALIASES_TABLE);
		$this->setMyPrimaryKey("alias_id");

	}
	
	/**
	 * Update an alias record
	 * @var string $alias
	 * @var integer $member_id
	 */
	function update($data) {

		$this->setAliasName($data['alias_name']);

		/**
		 * Two members may not share alias names
		 */ 
		$record = $this->getByName($this->getAliasName());
		$member_id = $record['alias_member_id'];

		if (!isset($member_id)) {		
			$this->doUpdate ($data['alias_id'],
			                 array('alias_name'=>$this->getAliasName(),
			                       'alias_member_id'=>$data['alias_member_id']));
		} else {
			return 0;
		}

		return 1;
	}
	
	/**
	 * Get the alias record. We need to clean up alias names prior to querying
	 * @var string $alias_name
	 */
	function getAliasByName($alias_name) {

		$this->setAliasName($alias_name);
		
		return $this->getByName($this->getAliasName());
	}
	
	/**
	 * Insert an alias into the database
	 * @var string $alias
	 * @var integer $member_id
	 */
	function insert($data) {

		$this->setAliasName($data['alias_name']);

		/**
		 * Two members may not share alias names
		 */ 
		$record = $this->getByName($this->getAliasName());
		$member_name = $record['member_name'];

		if (!isset($member_name)) {		
			$this->doInsert (array('alias_name'=>$this->getAliasName(),
			                       'alias_member_id'=>$data['alias_member_id']));
		} else {
			return 0;
		}

		return 1;
	}

	/**
	 * Delete an alias
	 */
	function delete ($data) {}

	/**
	 * Get all alias & member name pairs
	 */
	function getRecord ($cmd=CTRT_GET_ALL,$param="") {
        global $db;

        /**
         * Generate the list of aliases
         */
        $sql =   "SELECT alias_id, alias_name, alias_member_id, member_name
                    FROM " . $this->getMyTable() . ",
                         " . MEMBERS_TABLE . "
                   WHERE member_id = alias_member_id ";
		
		switch ($cmd) {
			case CTRT_GET_ALL:
				break;
			
			case CTRT_GET_BY_ID:
				$sql .= "AND alias_id = ".$param." ";
				break;
					
			case CTRT_GET_BY_NAME:
				$this->setAliasName($param);
				$sql .= "AND alias_name = '".mysql_real_escape_string($this->getAliasName())."' ";
				break;
			
			default: break;
		}
		$sql .= "ORDER BY alias_name";
        $result = $db->query($sql);

		$playeraliases = array();
        while ( $row = $db->fetch_record($result) )
        {
			$playeraliases[] = array("alias_id"=>$row["alias_id"],
			                         "alias_name"=>$row["alias_name"],
								     "alias_member_id"=>$row["alias_member_id"],
								     "member_name"=>$row["member_name"]
								    );
        }
        $db->free_result($result);
		
		switch ($cmd) {
			case CTRT_GET_ALL:
				break;
			
			case CTRT_GET_BY_NAME:
			case CTRT_GET_BY_ID:
				$playeraliases = $playeraliases[0];
				break;
			
			default: break;
		}
		
		return $playeraliases;
	}
	
	/**
	 * Clean and set alias_name
	 * @var string alias_name
	 */
	function setAliasName($alias_name) {
		$this->_alias_name = $this->capitalize($alias_name);
		return 1;
	}

	/**
	 * Clean and set alias_name
	 * @return string $_alias_name
	 */
	function getAliasName() {
		return $this->_alias_name;
	}
}
?>